﻿@{
    Layout = "../Shared/_InnerPage.cshtml";
}
@model XoohooViewModelItem<User>
@section TitleContent{
    @Html.SiteTitle("系统管理", "用户管理", "用户信息管理", "用户编辑")
}
@section HeadCustom{
    <style type="text/css">
        div.status div
        {
            float: left;
            width: 50px;
        }
    </style>
    <script language="javascript" type="text/javascript">

        $(document).ready(function () {
            //Tabs
            $('#tabs').tabs();

            $(":checkbox[@@name='Permissions']").each(function () {
                $(this).click(function () {
                    checkedNode(this);
                });
            });
            function checkedNode(self) {
                var parentID = $(self).attr("parentID");
                var selfValue = $(self).attr("value");
                var isChecked = $(self).attr("checked");
                var childNode = $(":checkbox[parentID='" + selfValue + "']");
                //如果是不是顶级节点
                if (parentID) {
                    //如果当前节点选中，则父节点必须选中,并且子节点必须选中
                    if (isChecked == true) {
                        //选中父节点
                        checkedParentNode(self);
                        childNode.each(function () {
                            $(this).attr("checked", "checked");
                            checkedNode(this);
                        });
                    }
                    else {
                        childNode.each(function () {
                            $(this).removeAttr("checked");
                            checkedNode(this);
                        });
                    }

                } else {//如果是顶级节点，将所有子节点的checked设置与当前节点checked值相同
                    childNode.each(function () {
                        if (isChecked == true) {
                            $(this).attr("checked", "checked");
                        } else {
                            $(this).removeAttr("checked");
                        }
                        checkedNode(this);
                    });
                }
            }
            function checkedParentNode(self) {
                var parentID = $(self).attr("parentID");
                if (parentID) {
                    var parentNode = $(":checkbox[value='" + parentID + "']");
                    parentNode.attr("checked", "checked");
                    checkedParentNode(parentNode);
                }
            }
        });
    </script>
}
@section NavigationContent
{
    当前位置：用户管理 &gt;&gt; 用户信息管理 &gt;&gt; 用户编辑
}
@{
    string returnUrl = Model.ReturnUrl;
    SelectList userGroups = Model.GetModelItem<SelectList>("UserGroups");
    SelectList status = Model.GetModelItem<SelectList>("UserStatus");


    UserInputAdd userInputAdd = null;
    UserInputEdit userInputEdit = null;
    UserInput user = UserInput.FromUser(Model.Item);
    bool isEdit = user != null;
    
    //特定权限
    var roles = Model.GetModelItem<SelectList>("Roles");
    //特定权限
    var permissions = Model.GetModelItem<IEnumerable<Permission>>("Permissions");

    var inputStyle = new { @class = "input" };

    Html.BeginRouteForm(isEdit ? "Admin.Membership.User.UserEditSave" : "Admin.Membership.User.UserAddSave", new { ReturnUrl = returnUrl }, FormMethod.Post, new { @id = "UserSaveForm", @name = "UserSaveForm" });
}
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">基本资料</a></li>
            <li><a href="#tabs-2">特定角色</a></li>
            <li><a href="#tabs-3">特定权限</a></li>
        </ul>
        <div id="tabs-1">
            <table cellpadding="4" cellspacing="0" class="tbl">
                <tr>
                    <td class="tit">
                        所属用户组：
                    </td>
                    <td class="con">
                        @Html.DropDownListFor(user, item => item.UserGroupID, userGroups, "-请选择-")
                    </td>
                    <td>
                        @Html.ValidationMessageFor(user, item => item.UserGroupID, "请选择用户组")
                    </td>
                </tr>
                <tr>
                    <td class="tit">
                        状态：
                    </td>
                    <td class="con">
                        @Html.RadioButtonListFor(user, item => item.Status, status,null, new { @class="status"})
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="tit">
                        用户名：
                    </td>
                    <td class="con">
                        @Html.TextBoxFor(user, item => item.Username, inputStyle)
                    </td>
                    <td>
                        @Html.ValidationMessageFor(user, item => item.Username, "请输入用户名")
                    </td>
                </tr>
                <tr>
                    <td class="tit">
                        昵称：
                    </td>
                    <td class="con">
                        @Html.TextBoxFor(user, item => item.DisplayName, inputStyle)
                    </td>
                    <td>
                        @Html.ValidationMessageFor(user, item => item.DisplayName, "请输入昵称")
                    </td>
                </tr>
                <tr>
                    <td class="tit">
                        邮件：
                    </td>
                    <td class="con">
                        @Html.TextBoxFor(user, item => item.Email, inputStyle)
                    </td>
                    <td>
                        @Html.ValidationMessageFor(user, item => item.Email, "请输入邮件")
                    </td>
                </tr>
                <tr>
                    <td class="tit">
                        登录密码：
                    </td>
                    <td>
                        @{
                            //其实对于PasswordFor，使用userInputAdd和userInputEdit都可以
                            //因为使用UserInputAdd类型和UserInputEdit类型都有Password属性
                            
                          if (!isEdit)
                          {
                            WriteLiteral(Html.PasswordFor(userInputAdd, item => item.Password, inputStyle));
                          }
                          else
                          {
                            WriteLiteral(Html.PasswordFor(userInputEdit, item => item.Password, inputStyle));
                          }
                        } 
                    </td>
                    <td>
                        @{
                          if (!isEdit)
                          {
                            WriteLiteral(Html.ValidationMessageFor(userInputAdd, item => item.Password, "请输入登录密码"));
                          }
                          else
                          {
                            WriteLiteral(Html.ValidationMessageFor(userInputEdit, item => item.Password,"如果不修改，请保持为空"));
                          }
                        } 
                    </td>
                </tr>
                <tr>
                    <td class="tit">
                        确认密码：
                    </td>
                    <td>
                        @{
                          if (!isEdit)
                          {
                            WriteLiteral(Html.PasswordFor(userInputAdd, item => item.PasswordConfirm, inputStyle));
                          }
                          else
                          {
                            WriteLiteral(Html.PasswordFor(userInputEdit, item => item.PasswordConfirm, inputStyle));
                          }
                        } 
                    </td>
                    <td>
                        @{
                            if (!isEdit)
                            {
                                WriteLiteral(Html.ValidationMessageFor(userInputAdd, item => item.PasswordConfirm, "请输入确认密码"));
                            }
                            else
                            {
                                WriteLiteral(Html.ValidationMessageFor(userInputEdit, item => item.PasswordConfirm, "如果不修改，请保持为空"));
                            }
                        } 
                    </td>
                </tr>
            </table>
        </div>
        <div id="tabs-2">
            <table cellpadding="4" cellspacing="0" class="tbl">
                <tr>
                    <td class="tit">
                        特定角色：
                    </td>
                    <td>
                        @Html.CheckBoxListFor(user,item=>item.Roles,roles)
                    </td>
                </tr>
            </table>
        </div>
        <div id="tabs-3">
            <table cellpadding="4" cellspacing="0" class="tbl">
                <tr>
                    <td class="tit">
                        特定权限：
                    </td>
                    <td>
                        @{
                            if (!permissions.IsNullOrEmpty())
                            {
                                var isChecked = false;
                                foreach (var p in permissions)
                                {
                                    isChecked = isEdit && user.Permissions.Any(up => up == p.PermissionID);
                                    WriteLiteral("{0}&nbsp;{1}<br/>".FormatWith(Html.CheckBox("Permissions", p.PermissionID.ToString(), isChecked, new { parentID=p.ParentID}),p.PermissionName));
                                }
                            }
                        }
                    </td>
                </tr>
            </table>
        </div>
        <div id="tabsFooter" style="text-align: center">
            @Html.ImageButton("btnSubmit", "Styles/Images/btnSubmit.jpg")
        </div>
    </div>
@{Html.EndForm();}
